﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Web.UI.HtmlControls;
using SMSROUTER.Business;
using Utils;
using System.Data;

namespace SMSROUTER.UI
{
    public partial class SmsMtStatistics : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        void paging_PagingChange(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            pagingbottom.CurrentPage = (sender as UserControl.ucPaging).CurrentPage;
            BindDataSmsStatistic();
            pagingtop.Visible = pagingbottom.Visible = (sender as UserControl.ucPaging).TotalPage > 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                pagingtop.PageDisplay = pagingbottom.PageDisplay = 6;
                BindDropDownListData(ddlShortCode, DB.SmsShortCodes.ToList(), "ShortCode", "ShortCode", new ListItem(""));
            }
        }

        private void BindDataSmsStatistic()
        {
            try
            {
                string shortCode = null;
                string destAddr = null;
                string status = null;
                int currentPage = pagingtop.CurrentPage;
                int pageSize = pagingtop.PageSize;
                int totalRow = 0;
                int totalMessage = 0;
                //int totalMessage = 0;
                shortCode = ddlShortCode.SelectedValue;
                destAddr = txtPhoneNumber.Text.Trim();
                DateTime? fromTime = txtFromDate.Text.ToNullableDateTime();
                DateTime? toTime = txtToDate.Text.ToNullableDateTime();
                status = ddlStatus.SelectedValue;
                grvData.DataSource = SmsOutStatistic.Paging(currentPage, pageSize, ref totalRow, ref totalMessage, shortCode, destAddr, fromTime, toTime, status);
                pagingtop.TotalRecord = pagingbottom.TotalRecord = totalRow;
                pagingtop.Visible = pagingbottom.Visible = pagingtop.TotalPage > 1;
                grvData.DataBind();
                ltrInfo.Text = "Tổng cộng bản tin: " + totalMessage;
            }
            catch(Exception ex) {
                HandleException(ex);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            pagingtop.PagingChange -= new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange -= new EventHandler(paging_PagingChange);
            pagingtop.CurrentPage = pagingbottom.CurrentPage = 1;
            pagingtop.PagingChange += new EventHandler(paging_PagingChange);
            pagingbottom.PagingChange += new EventHandler(paging_PagingChange);
            BindDataSmsStatistic();
        }

        protected void tSortable_length_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                PageSize = Convert.ToInt32(tSortable_length.SelectedValue);
                pagingtop.PageSize = pagingbottom.PageSize = PageSize;
                BindDataSmsStatistic();
            }
            catch { }
        }

        protected void btnRetry_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("TypeName", typeof(long));
                foreach (GridViewRow row in grvData.Rows)
                {
                    CheckBox chk = row.FindControl("chkItem") as CheckBox;
                    if (chk != null && chk.Checked)
                    {
                        dt.Rows.Add(chk.ToolTip.ToLong());
                    }
                }
                SmsOutStatistic.Retry(dt);
                BindDataSmsStatistic();
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
        }
    }
}